package cn.net.sdjx.admin.controller;

import cn.net.sdjx.common.POIUtil;
import cn.net.sdjx.common.Pagination;
import cn.net.sdjx.param.WaiterParam;
import cn.net.sdjx.pojo.Waiter;
import cn.net.sdjx.service.WaiterService;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class IndexController {

  @Autowired
  WaiterService waiterService;

  @RequestMapping("/select")
  public ModelAndView select(WaiterParam vsp) {
    ModelAndView mv = new ModelAndView("waiter/waiter");
    Pagination<Waiter> page = waiterService.getPageList(vsp);
    mv.addObject("page", page);
    mv.addObject("vsp", vsp);
    return mv;
  }

  @RequestMapping("see")
  public ModelAndView see(Integer id, Integer pageNum) {
    ModelAndView mv = new ModelAndView("waiter/see");
    Waiter waiter = waiterService.selectByPrimaryKey(id);// 根据id查人员
    mv.addObject("waiter", waiter);
    mv.addObject("pageNum", pageNum);
    return mv;
  }

  @RequestMapping("/javapoi")
  public String javapoi(HttpServletRequest req, MultipartFile file)
      throws IOException, ParseException {
    List<String[]> list = POIUtil.readExcel(file);
    for (int i = 0; i < list.size(); i++) {
      Waiter waiter = new Waiter();
      waiter.setAddress(list.get(i)[1].replace(" ", ""));//地区
      waiter.setName(list.get(i)[2].replace(" ", ""));// 名字
      waiter.setSex(list.get(i)[3].replace(" ", ""));// 性别
      waiter.setIdcard(list.get(i)[4].replace(" ", ""));// 身份证
      waiter.setAffiliation(list.get(i)[5].replace(" ", ""));// 所属机构
      waiter.setType(list.get(i)[6].replace(" ", ""));// 公司类型
      waiter.setQualId(list.get(i)[7]);// 资质编号
      waiter.setMedalId(list.get(i)[8]);// 铜牌编号
      waiter.setPersion(list.get(i)[9].replace(" ", ""));// 个人情况
      waiterService.insertSelective(waiter);
    }
    return "redirect:select.do?pageNum=1&numPerPage=10";
  }

  @RequestMapping("seeOne")
  public ModelAndView seeOne(Integer id) {
    ModelAndView mv = new ModelAndView("printing/page");
    Waiter waiter = waiterService.selectByPrimaryKey(id);
    mv.addObject("waiter", waiter);
    return mv;
  }

  @RequestMapping("delete")
  public ModelAndView delete() {
    ModelAndView mv = new ModelAndView("waiter/waiter");
    boolean r = waiterService.delete();
    return mv;
  }
}
